package co.com.inventarios.persistencia;

import java.util.List;
import java.util.logging.Level;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import co.com.inventarios.modelo.Tipoiva;
import co.com.inventarios.modelo.Unimed;
import co.com.inventarios.modelo.Vinculado;

public class QueryUnimedDAO {

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	
	public List<Unimed> findByPropertyAll(String propertyName, Object value ,String espresion) {

		EntityManagerHelper.log("finding Unimed instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Unimed model where model."
					+ propertyName + " LIKE '%" + value + "%'  "+espresion+" ";
			Query query = getEntityManager().createQuery(queryString);

			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	
	public List<Unimed> findBy_all(String propertyName) {

		
		try {
			getEntityManager().clear();
			final String queryString = "select model from Unimed model "+propertyName+" ";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	public Unimed findByPropertyActivo(String propertyName, Object value) {
		// TODO Auto-generated method stub
		EntityManagerHelper.log("finding Unimed instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Unimed model where model.umdactivo = 1 AND model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
			
			
			Unimed result = new Unimed(); 
			if (query.getResultList().size() > 0){
				result = (Unimed)query.getSingleResult();
			}else{
				result = null;
			}
				
			return result;
			
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
	public List<Unimed> findByPropertyActivoLista(String propertyName) {
		try {
			getEntityManager().clear();
			final String queryString = "select model from Unimed model where model.umdactivo = 1 "+propertyName+" ";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}
	
}
